/**
 * 工具包
 *
 */

import dayjs from "dayjs";
/**
 * 对象参数转url query
 * @param {object} params 需要转换的对象
 * @return
 */
export const paramsToQuery = (params = {}) => {
  return Object.keys(params).length > 0
    ? Object.keys(params)
        .map(
          (k, i) =>
            `${i === 0 ? "?" : ""}${encodeURIComponent(k)}=${encodeURIComponent(
              params[k]
            )}`
        )
        .join("&")
    : "";
};

/**
 * 格式化时间
 * @param  {Number} time 时间戳
 */
export function formatTime(diff: any) {
  diff /= 1000;
  if (!diff) {
    return "-";
  }
  if (diff < 30) {
    return "刚刚";
  } else if (diff < 3600) {
    return `${Math.floor(diff / 60)}分钟`;
  } else if (diff < 3600 * 24) {
    return `${Math.floor(diff / 3600)}小时`;
  } else if (diff < 3600 * 24 * 2) {
    return "1天";
  } else if (diff >= 3600 * 24 * 2) {
    return `${Math.floor(diff / (3600 * 24))}天`;
  }
  return "-";
}

/**
 * 获取随机数
 * @param {String} str 随机数开头字母
 * @param {Number} n 随机数个数
 * @return {String} 生成的随机数
 */
export function getRandomNum(str = "", n = 12) {
  if (typeof n === "string") {
    n = Number(n);
  }
  n += 2;
  return str + String(Math.random()).slice(2, n);
}

/**
 * 数字转大写
 * @export
 * @param {*} str
 * @returns
 */
export function numberChinese(str) {
  try {
    let num: any = parseFloat(str);
    let strOutput = "";
    let strUnit = "仟佰拾亿仟佰拾万仟佰拾元角分";
    num += "00";
    const intPos = num.indexOf(".");
    if (intPos >= 0) {
      num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
    }
    strUnit = strUnit.substr(strUnit.length - num.length);
    for (let i = 0; i < num.length; i++) {
      strOutput +=
        "零壹贰叁肆伍陆柒捌玖".substr(num.substr(i, 1), 1) +
        strUnit.substr(i, 1);
    }
    return strOutput
      .replace(/零角零分$/, "整")
      .replace(/零[仟佰拾]/g, "零")
      .replace(/零{2,}/g, "零")
      .replace(/零([亿|万])/g, "$1")
      .replace(/零+元/, "元")
      .replace(/亿零{0,3}万/, "亿")
      .replace(/^元/, "零元");
  } catch (error) {
    return str;
  }
}

/**
 * 设置url参数
 * @param {}
 * @return
 */
/**
 * 设置 url 参数
 */
function setQueryParameter(uri, key, value) {
  if (!value) {
    return uri;
  }
  const re = new RegExp(`([?&])${key}=.*?(&|$)`, "i");
  const separator = uri.indexOf("?") !== -1 ? "&" : "?";

  if (uri.match(re)) {
    return uri.replace(re, `$1${key}=${value}$2`);
  } else {
    return `${uri + separator + key}=${value}`;
  }
}
export function updateQueryStringParameter(key, value) {
  const newurl = setQueryParameter(window.location.href, key, value);
  window.history.replaceState(
    {
      path: newurl,
    },
    "",
    newurl
  );
}

/**
 * 删除指定search参数
 */
export function delQueStr(url, ref) {
  // 删除指定参数值
  let str = "";
  let newurl = "";
  let hash = "";
  if (url.indexOf("#") !== -1) {
    hash = `#${url.split("#")[1]}`;
    url = url.split("#")[0];
  }
  if (url.indexOf("?") !== -1) str = url.substr(url.indexOf("?") + 1);
  else return url;
  let arr: any = "";
  let returnurl = "";
  if (str.includes("&")) {
    arr = str.split("&");
    for (const i in arr) {
      if (arr[i].split("=")[0] !== ref) {
        returnurl = `${returnurl + arr[i].split("=")[0]}=${
          arr[i].split("=")[1]
        }&`;
      }
    }
    newurl = `${url.substr(0, url.indexOf("?"))}?${returnurl.substr(
      0,
      returnurl.length - 1
    )}`;
    window.history.replaceState(
      {
        path: newurl + hash,
      },
      "",
      newurl + hash
    );
  } else {
    arr = str.split("=");
    if (arr[0] === ref) {
      newurl = url.substr(0, url.indexOf("?"));
      window.history.replaceState(
        {
          path: newurl + hash,
        },
        "",
        newurl + hash
      );
    } else return url;
  }
}
